﻿using AppMVVM.DataAccess;
using AppMVVM.Models;
using AppMVVM.WCFWebRoleService;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppMVVM.ViewModels
{
    class FeedReaderViewModel : ViewModelBase
    {
        private DataDistributorClient _dataDistributor;

        private UserData _user;

        private IEnumerable<UserSubscriptionInformation> _subscriptions;

        readonly List<string> _categoryNameSet = new List<string> { "Sources" };
        readonly CategoryRepository _categoryRepository;

        public FeedReaderViewModel(UserData userData)
        {
            Contract.Ensures(userData != null);

            _user = userData;
            _dataDistributor = DataDistributor.Instance;

            _categoryRepository = new CategoryRepository(_categoryNameSet);
            AllCategories = new ObservableCollection<Category>(_categoryRepository.GetCategories());

            var generalCategory = AllCategories.Single(c => c.Name == "Sources");
            generalCategory.Sources = new ObservableCollection<Source>(ActiveSubscriptions.Select(s => new Source(s)));
        }

        public ObservableCollection<Category> AllCategories
        {
            get;
            private set;
        }

        private IEnumerable<UserSubscriptionInformation> Subscriptions
        {
            get
            {
                var subscriptions = _dataDistributor.GetSubscriptions(_user.UserId);
                _subscriptions = new ObservableCollection<UserSubscriptionInformation>(subscriptions);
                return _subscriptions;
            }
        }

        private IEnumerable<UserSubscriptionInformation> ActiveSubscriptions
        {
            get { return Subscriptions.Where(s => s.IsSubscribe == true); }
        }
    }
}
